package white_list

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/cars"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)
	where["1= ? and deleted_at is null"] = 1
	//查询信息
	var carsWhiteListStruct cars.CarsWhiteListStruct
	var carsWhiteListData []cars.CarsWhiteListStruct
	model.GetModelData(
		model.Db,
		carsWhiteListStruct.TableName(),
		&carsWhiteListData,
		model.QueryOptions{
			Page:       page,
			PageSize:   pageSize,
			Conditions: where,
			OrderBy:    "id desc",
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range carsWhiteListData {
		returnData = append(returnData, map[string]interface{}{
			"id":           v.Id,
			"name":         v.Name,
			"mobile":       v.Mobile,
			"car_number":   v.CarNumber,
			"car_no_color": v.CarNoColor,
			"car_type":     v.CarType,
			"order_type":   v.OrderType,
			"out_time":     v.OutTime.Format("2006-01-02 15:04:05"),
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		carsWhiteListStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
